//
//  AddMemorandumViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/1/16.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "AddMemorandumViewController.h"

@interface AddMemorandumViewController ()<UITextViewDelegate>
@property (nonatomic,weak) JSTextView *releaseContentTextView;
@property (nonatomic,assign) BOOL isEdit;   //是否可编辑
@end

@implementation AddMemorandumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (!strIsEmpty(self.memorandumInfo.add_time)) {
        self.title = [self.memorandumInfo.add_time substringToIndex:10];
    }else{
        self.title = @"备忘录";
    }
    if (strIsEmpty(self.memorandumInfo.memorandumId)) {
        self.isEdit = YES;
    }else{
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:@"编辑" target:self selector:@selector(editAdvice)];
    }
    [self creatFootView];
}
- (void)editAdvice{
    self.isEdit = YES;
    [self creatFootView];
    [self.tableView reloadData];
}
- (void)creatFootView{
    UIView *footView = [[UIView alloc]init];
    footView.frame = CGRectMake(0, 0, kScreenW, 100);
    footView.backgroundColor = [UIColor whiteColor];
    
    CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(10, 10, kScreenW - 20, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"提交"];
    [completeButton addTarget:self action:@selector(addMemorandum) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:completeButton];
    completeButton.userInteractionEnabled = self.isEdit;
    completeButton.backgroundColor = self.isEdit ? KBACKNAVIGATIONCOLOR:[UIColor lightGrayColor];
    self.tableView.tableFooterView = footView;
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    JSTextView *releaseContentTextView = [[JSTextView alloc] initWithFrame:CGRectMake(10, 10, kScreenW - 20, 80)];
    releaseContentTextView.font = [UIFont systemFontOfSize:16];
    releaseContentTextView.textColor = KTEXTMAINCOLOR;
    releaseContentTextView.autocorrectionType = UITextAutocorrectionTypeNo;  //取消纠错
    releaseContentTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;  //取消首字母大写
    releaseContentTextView.returnKeyType = UIReturnKeyDefault;  //return改成done
    [cell.contentView addSubview:releaseContentTextView];
    releaseContentTextView.text = strIsEmpty(self.memorandumInfo.content)?@"":self.memorandumInfo.content;
    self.releaseContentTextView = releaseContentTextView;
    releaseContentTextView.myPlaceholder = @"这里写备忘录";
    releaseContentTextView.userInteractionEnabled = self.isEdit;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
#define mark - 提交备忘录
- (void)addMemorandum{
    [self.tableView endEditing:YES];
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if (!strIsEmpty(self.memorandumInfo.memorandumId)) {
         [dic setValue:self.memorandumInfo.memorandumId forKey:@"id"];   //动态的内容详情
    }
    [dic setValue:self.releaseContentTextView.text forKey:@"content"];   //动态的内容详情
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppInterestSaveMemory] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [Global showActivityWithView:self.view withText:@"发送成功"];
            if (self.sendMemorandumBlock) {
                self.sendMemorandumBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
